<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 16/4/19
 * Time: 上午9:55
 */
$api_service_path = __DIR__ . "/../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();

$car_brands = array(
    '一汽',
    '三菱',
    '东南',
    '东风',
    '中兴',
    '中华',
    '丰田',
    '五菱',
    '保时捷',
    '克莱斯勒',
    '兰博基尼',
    '兰吉雅',
    '凯迪拉克',
    '别克',
    '力帆',
    '双龙',
    '吉利',
    '吉利全球鹰',
    '吉利华普',
    '吉普',
    '名爵',
    '哈飞',
    '夏利',
    '大众',
    '大发',
    '大宇',
    '奇瑞',
    '奇瑞威麟',
    '奇瑞瑞麟',
    '奔腾',
    '奔驰',
    '奥迪',
    '宝马',
    '宝马MINI',
    '帝豪',
    '开瑞',
    '捷豹',
    '斯巴鲁',
    '斯柯达',
    '斯玛特',
    '日产',
    '昌河',
    '本田',
    '标致',
    '欧宝',
    '比亚迪',
    '江淮',
    '沃尔沃',
    '法拉利',
    '海马',
    '现代',
    '福特',
    '福田',
    '英菲尼迪',
    '荣威',
    '莲花',
    '菲亚特',
    '讴歌',
    '起亚',
    '路虎',
    '道奇',
    '金杯',
    '铃木',
    '长丰',
    '长城',
    '长安',
    '阿斯顿马丁',
    '雪弗兰',
    '雪铁龙',
    '雷克萨斯',
    '雷诺',
    '马自达'
);

foreach($car_brands as $_car_brand){
    $oem_url = 'http://www.varta-automotive.cn/index.php/tools/blocks/find_a_battery/lookup.php?make='.$_car_brand;
    $oems = get_varta_info($oem_url);
    $oems_array = explode('"',$oems);
    for($i = 3 ; ; $i = $i + 2){
        if(array_key_exists($i,$oems_array)){
            $model_url = $oem_url.'&oem='.$oems_array[$i];
            $models = get_varta_info($model_url);
            $models_array = explode('"',$models);
            for($j = 3 ; ;$j = $j + 2){
                if(array_key_exists($j,$models_array)){
                    echo $_car_brand.','.$oems_array[$i].','.$models_array[$j]."\n";
                }else{
                    break;
                }
            }
        }else{
            break;
        }
    }
}

function get_varta_info($url){
    while(1){
        try{
            $response = Httpful::get($url)
                ->timeoutIn(5)
                ->sendsForm()->send();
            break;
        }catch (Exception $e) {
            var_dump($e);
        }
    }
    return $response->body;
}

